/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.xy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xy.constants.RabbitConstant;
import com.xy.constants.RedisConstant;
import com.xy.dto.ViewerSortDTO;
import com.xy.entity.TimeStageEntity;
import com.xy.entity.ViewerEntity;
import com.xy.entity.ViewerSortEntity;
import com.xy.request.QueueFaceDTO;
import com.xy.service.ITimeStageService;
import com.xy.service.IViewerService;
import com.xy.vo.ViewerSortVO;
import com.xy.excel.ViewerSortExcel;
import com.xy.mapper.ViewerSortMapper;
import com.xy.service.IViewerSortService;
import com.xy.vo.ViewerVO;
import com.xy.wrapper.ViewerSortWrapper;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 用户分类管理表 服务实现类
 *
 * @author BladeX
 * @since 2023-07-18
 */
@Service
@AllArgsConstructor
public class ViewerSortServiceImpl extends BaseServiceImpl<ViewerSortMapper, ViewerSortEntity> implements IViewerSortService {

	private final ITimeStageService timeStageService;
	private final BladeRedis bladeRedis;
	private final RabbitTemplate rabbitTemplate;
	@Override
	public Boolean submitViewerSort(ViewerSortDTO viewerSort) {
		ViewerSortEntity typeEntity = Objects.requireNonNull(BeanUtil.copy(viewerSort, ViewerSortEntity.class));
		boolean orUpdate = this.saveOrUpdate(typeEntity);
		IViewerService viewerService = SpringUtil.getBean(IViewerService.class);
		if(typeEntity.getFaceStatus() == 0){
			List<ViewerEntity> list = viewerService.list(Wrappers.<ViewerEntity>lambdaQuery().eq(ViewerEntity::getViewerTypeId, typeEntity.getId()).eq(ViewerEntity::getFaceRegister, 1));
			list.forEach(o->{
				if(Func.isEmpty(o.getHeadUrl())){
					return;
				}
				QueueFaceDTO queueFaceDTO = BeanUtil.newInstance(QueueFaceDTO.class);
				queueFaceDTO.setTicketId(o.getId().toString());
				queueFaceDTO.setGroupId(o.getExhibitionId().toString());
				queueFaceDTO.setPhotoUrl(o.getHeadUrl());
				queueFaceDTO.setTicketName(o.getName());
				//  消息队列腾讯
				rabbitTemplate.convertAndSend(RabbitConstant.TOPIC_TENCENT_ADD_FACE_EXCHANGE,RabbitConstant.TOPIC_TENCENT_ADD_FACE_ROUTING_KEY_ONE, JSONObject.toJSONString(queueFaceDTO));
			});
		}
		if(Func.isEmpty(viewerSort.getTimeList())){
			return  orUpdate;
		}
		LambdaQueryWrapper<TimeStageEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
		lambdaQueryWrapper.eq(TimeStageEntity::getViewerId,typeEntity.getId());
		lambdaQueryWrapper.eq(TimeStageEntity::getDataType,1L);
		timeStageService.remove(lambdaQueryWrapper);
		List<TimeStageEntity> timeVOList = viewerSort.getTimeList();
		timeVOList.forEach(o->{
			o.setViewerId(typeEntity.getId());
		});
		return timeStageService.saveBatch(timeVOList);
	}

	@Override
	public IPage<ViewerSortVO> selectViewerSortPage(IPage<ViewerSortVO> page, ViewerSortVO viewerSort) {
		return page.setRecords(baseMapper.selectViewerSortPage(page, viewerSort));
	}

	@Override
	public IPage<ViewerSortVO> listPage(Map<String, Object> viewerSort, Query query) {
		IPage<ViewerSortEntity> pages = this.page(Condition.getPage(query), Condition.getQueryWrapper(viewerSort,ViewerSortEntity.class));
		IPage<ViewerSortVO> page = ViewerSortWrapper.build().pageVO(pages);
		List<ViewerSortVO> voList = page.getRecords();
		voList.forEach(o->dataList(o));
		page.setRecords(voList);
		return page;
	}

	public void dataList(ViewerSortVO sortVO){
		String key = RedisConstant.TYPE_ID_EX_TYPE_NAME.concat(sortVO.getSortName()).concat(sortVO.getExhibitionId() + "");
		bladeRedis.set(key,sortVO);
		LambdaQueryWrapper<TimeStageEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(TimeStageEntity::getViewerId,sortVO.getId());
//		queryWrapper.eq(TimeStageEntity::getDataType, EumAdmin.DATA_TYPE.getCode());
		List<TimeStageEntity> timeEntities = timeStageService.list(queryWrapper);
		sortVO.setTimeList(timeEntities);
	}
	@Override
	public List<ViewerSortExcel> exportViewerSort(Wrapper<ViewerSortEntity> queryWrapper) {
		List<ViewerSortExcel> viewerSortList = baseMapper.exportViewerSort(queryWrapper);
		//viewerSortList.forEach(viewerSort -> {
		//	viewerSort.setTypeName(DictCache.getValue(DictEnum.YES_NO, ViewerSort.getType()));
		//});
		return viewerSortList;
	}

}
